class EmployeesController < ApplicationController
  # GET /employees
  # GET /employees.xml
  
  before_filter :login_required
  before_filter :session_expiry
  
  def index
    @employees = Employee.all
        
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @employees }
    end
    
  end

  def filter
    @query_name = "%"+params[:filter][:employee_name]+"%"
    @query_surname = "%"+params[:filter][:employee_surname]+"%"
    @query_address = "%"+params[:filter][:employee_address]+"%"
    @query_login = "%"+params[:filter][:employee_login]+"%"
    @query_warehouse = params[:filter][:employee_warehouse]
	if @query_warehouse == "" then @query_warehouse = '-1'
	end
    
    @employees = Employee.find(:all, :conditions => ["name like ? AND surname like ? AND address like ? AND login like ? AND (warehouse_id = ? OR ? = '-1')", 
                                                    @query_name, @query_surname, @query_address, @query_login, @query_warehouse, @query_warehouse])
  end
  
  # GET /employees/1
  # GET /employees/1.xml
  def show
    @employee = Employee.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @employee }
    end
  end

  # GET /employees/new
  # GET /employees/new.xml
  def new
    @employee = Employee.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @employee }
    end
  end

  # GET /employees/1/edit
  def edit
    @employee = Employee.find(params[:id])
  end

  # POST /employees
  # POST /employees.xml
  def create
    @employee = Employee.new(params[:employee])

    respond_to do |format|
      if @employee.save
        flash[:notice] = 'Employee was successfully created.'
        format.html { redirect_to(@employee) }
        format.xml  { render :xml => @employee, :status => :created, :location => @employee }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @employee.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /employees/1
  # PUT /employees/1.xml
  def update
    @employee = Employee.find(params[:id])

    respond_to do |format|
      if @employee.update_attributes(params[:employee])
        flash[:notice] = 'Employee was successfully updated.'
        format.html { redirect_to(@employee) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @employee.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /employees/1
  # DELETE /employees/1.xml
  def destroy
    @employee = Employee.find(params[:id])
    @employee.destroy

    respond_to do |format|
      format.html { redirect_to(employees_url) }
      format.xml  { head :ok }
    end
  end
  
  def passwordChange
    @employee = Employee.find(params[:id])
  end
end
